// Wweibo.go
package main

import (
	"encoding/json"
	"fmt"
	"log"
	"os"
	"runtime"
	"strconv"
	"time"

	"gitee.com/wang_cyi/TryEverything/public"
	"github.com/huichen/gobo"
)

//statuses/share 第三方分享链接到微博
//https://github.com/michaelliao/sinaweibopy
type config struct {
	AccessToken string `json:"access_token"`
	AppKey      string `json:"app_key"`
}

var Cfg config
var (
	weibo = gobo.Weibo{}
)

func init() {
	f, err := os.Open("conf.json")
	if err != nil {
		log.Fatalf("[ERROR] Fail to open config file: %s", err)
	}

	if err = json.NewDecoder(f).Decode(&Cfg); err != nil {
		log.Fatalf("[ERROR] Fail to decode config file: %s", err)
	}

	runtime.GOMAXPROCS(runtime.NumCPU())
}

//https://github.com/michaelliao/sinaweibopy/issues/58
//https://open.weibo.com/wiki/2/statuses/share
func PostWeibo(content string) {
	fmt.Println(Cfg.AppKey, ":", Cfg.AccessToken)
	params := gobo.Params{"source": Cfg.AppKey, "status": "#golang# " + content}
	if err := weibo.Call("statuses/share", "post", Cfg.AccessToken, params, nil); err != nil {
		log.Printf("[ERROR] PostWeibo: %s\n", err)
	}
}

func main() {

	var timestamp int64 = 1577808005 //2020-01-01
	begin := time.Unix(timestamp, 0)
	end := time.Now()
	cost := strconv.Itoa(public.TimeSubDays(end, begin))

	content := `即使你感觉 被撕成碎片
也要为自己缝一件新衣
今年是努力的第`

	content += cost + "天  https://github.com/wangcy6/weekly 每日复盘记录请点击>>> https://wangcy6.github.io"
	fmt.Println(content)

	PostWeibo(content)
}
